﻿#region Copyright

// The contents of this file are subject to the Mozilla Public License
//  Version 1.1 (the "License"); you may not use this file except in compliance
//  with the License. You may obtain a copy of the License at
//  
//  http://www.mozilla.org/MPL/
//  
//  Software distributed under the License is distributed on an "AS IS"
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//  License for the specific language governing rights and limitations under 
//  the License.
//  
//  The Initial Developer of the Original Code is Robert Smyth.
//  Portions created by Robert Smyth are Copyright (C) 2008.
//  
//  All Rights Reserved.

#endregion

using System.Windows.Media;
using NUnitGrid.Framework;
using NUnitGrid.Framework.Logging;
using NUnitGrid.Framework.Plugins;
using NUnitGrid.Framework.UI;


namespace NUnitGrid.GridClient.Plugins.GridClient.UI.Run
{
    public class StopButtonElementController : IUIChildPlugin, IButtonElementListener, IRunSessionListener,
                                               ILinkReadyListener
    {
        private readonly IStartRunListener listener;
        private readonly ILogger logger;
        private readonly IButtonElement stoprunButtonElement;
        private ChannelState channelState;
        private bool running;

        public StopButtonElementController(IButtonElement buttonElement, IStartRunListener listener, ILogger logger)
        {
            stoprunButtonElement = buttonElement;

            this.listener = listener;
            this.logger = logger;
        }

        void IButtonElementListener.OnClick()
        {
            logger.Info.Write("Stop button clicked.");
            listener.OnStopAllTasks();
        }

        void ILinkReadyListener.OnStateChange(ChannelState state)
        {
            channelState = state;
            UpdateButtons();
        }

        void IRunSessionListener.OnStarted()
        {
            running = true;
            UpdateButtons();
        }

        void IRunSessionListener.OnCompleted(int passedCount, int failedCount, bool taskError)
        {
            running = false;
            UpdateButtons();
        }

        void IRunSessionListener.OnMesssage(string format, params object[] args)
        {
        }

        public void OnStopping()
        {
            running = false;
            UpdateButtons();
        }

        public void OnTaskCompleted(string fixture, bool succeeded, string name, string output)
        {
        }

        public void Start(IHostView hostView)
        {
            hostView.AddChild(stoprunButtonElement);
            stoprunButtonElement.Label = "Press during run to stop a run.";
            UpdateButtons();
        }

        public void Stop(IHostView view)
        {
        }

        private void EnableStopButton(bool enable, string text)
        {
            stoprunButtonElement.IsEnabled = enable;
            stoprunButtonElement.ButtonText = text;
            if (enable)
            {
                stoprunButtonElement.ButtonBackground = new LinearGradientBrush(Colors.IndianRed, Colors.Crimson, 90.0);
                stoprunButtonElement.ButtonForeground = new SolidColorBrush(Colors.WhiteSmoke);
            }
            else
            {
                stoprunButtonElement.ButtonForeground = new SolidColorBrush(Colors.DarkGray);
            }
        }

        private void UpdateButtons()
        {
            if (running)
            {
                EnableStopButton(true, "Stop");
            }
            else if (channelState == ChannelState.NotConnected)
            {
                EnableStopButton(false, "Stop");
            }
            else
            {
                EnableStopButton(false, "Stop");
            }
        }
    }
}